// index.js
const http = require('../../utils/http')
const config = require('../../utils/config')
Page({
  data: {

    indexImgs: [], // 轮播图数据
    news: [], // 公告列表
    tags: ['限时特惠', '新品推荐','天猫国际'],
    data: [],
    test: 1,
    tagList: []
  },

  onLoad() {
    this.getAllData()
  },


  more() {
    wx.navigateTo({
      url: '/pages/notice/index',
    })
  },
  /***跳转商品详情页面 */
  gotoSeach(e) {
    console.log(e);
    wx.navigateTo({
      url: '../search/search',
    })
  },

  /***跳转商品详情页面 */
  gotoDetail(e) {
    console.log(e);
    wx.navigateTo({
      url: '../detail/detail?prodId=' + e.currentTarget.dataset.prodid,
    })
  },

  getAllData() {
    this.getBannerList()
    this.getNoticeList()
    this.getData()
    // this.getCartCount()
  },

  getData() {
    var data = []
    this.data.tags.forEach((tag) => {
      var page = this
      var params = {
        url: "/goods?category=" + tag,
        method: "GET",
        callBack(res) {
          data.push(res.slice(0, 20));
          page.setData({
            data: data
          })
        },
        errCallBack(res) {}
      }
      http.request(params)
    })

  },

  /***
   * 获取购物车的数量角标
   */
  getCartCount() {
    var page = this
    var params = {
      url: "/cart?userId=1",
      method: "GET",
      data: {},
      callBack(res) {
        console.log(res)
      },
      errCallBack(res) {}
    }
    http.request(params)
  },


  // 获取轮播图数据
  getBannerList() {
    var page = this
    var params = {
      url: "/image?goodsId=1103",
      method: "GET",
      data: {},
      callBack(res) {
        console.log(res)
        page.setData({
          indexImgs: res
        })
      },
      errCallBack(res) {}
    }
    http.request(params)
  },


  // 获取公告列表
  getNoticeList() {
    var page = this
    var params = {
      url: '/goods?category=广播',
      method: "GET",
      data: {},
      callBack(res) {
        console.log(res);
        page.setData({
          news: res
        })
      },
      errCallBack(res) {}
    }
    http.request(params)
  },

  getTagProd(id, index) {
    console.log(index);
    var page = this
    var params = {
      url: "/goods?category=新品推荐",
      method: "GET",
      data: {
        tagId: id,
        size: 6
      },
      callBack: (res) => {
        var taglist = this.data.tagList;
        taglist[index].prods = res.records

        this.setData({
          tagList: taglist,
        });
      },
      errCallBack(res) {}
    }
    http.request(params)
  }


})